package com.kzj.mls.activity;

import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kzj.mls.R;
import com.kzj.mls.beans.CouponBean;
import com.kzj.mls.global.App;
import com.kzj.mls.global.Constant;
import com.kzj.mls.recyclerCard.basic.MaterialListView;
import com.kzj.mls.recyclerCard.basic.SuperSwipeRefresh;
import com.kzj.mls.recyclerCard.card.other.CouponsCard;
import com.kzj.mls.request.BaseResponse;
import com.kzj.mls.request.ICallBackForRequestServer;
import com.kzj.mls.request.RequestServerManager;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

/**
 * Created by xyg on 2015/11/3.
 */
public class CouponsActivity extends GBaseActivity implements SuperSwipeRefresh.OnRefreshReceyerListener, SuperSwipeRefresh.OnLoadMoreListener {
    private MaterialListView material_listview;
    private SuperSwipeRefresh superSwipeRefresh;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.template_superswiperefresh_materlistview);
        findViewByIds();
        initDatas();
        fillArrayInListView();
        TaskGetCouponsList(page, false);
    }

    @Override
    protected void findViewByIds() {
        material_listview = (MaterialListView) findViewById(R.id.material_listview);
        superSwipeRefresh = (SuperSwipeRefresh) findViewById(R.id.superSwipeRefresh);

        superSwipeRefresh.setPageSize(5);
        superSwipeRefresh.setView(this, material_listview);
        superSwipeRefresh.setEnableLoadMore(true);
        superSwipeRefresh.setOnLoadMoreListener(this);
        superSwipeRefresh.setOnRefreshReceyerListener(this);
    }

    @Override
    protected void initDatas() {
        setTopTitle("我的优惠劵");
        setLeftImg(View.VISIBLE, R.drawable.back);

    }

    @Override
    protected void fillArrayInListView() {

    }

    protected void fillArrayInListView(List<CouponBean> couponBeans, boolean isLoad) {
        if (!isLoad) {
            material_listview.clear();
            page = 2;
        } else {
            page++;
        }
        for (int i = 0; i < couponBeans.size(); i++) {
            CouponsCard couponsCard = new CouponsCard(this);
            couponsCard.setCouponBean(couponBeans.get(i));
            material_listview.add(couponsCard);
        }
    }

    private void TaskGetCouponsList(int page, final boolean isLoad) {
        RequestServerManager.getInstance().handleMethod((GBaseActivity) getActivity(), material_listview, superSwipeRefresh, isLoad,
                Constant.getRootUrl() + Constant.Action.Action_Coupon_List, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_Coupon_List, page + ""), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        JSONObject jsonObject = (JSONObject) result.getRetval();
                        List<CouponBean> couponBeans = JSON.parseArray(jsonObject.getString("coupon_list"), CouponBean.class);
                        fillArrayInListView(couponBeans, isLoad);
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }

    @Override
    public void loadMore(int itemsCount, int maxLastVisiblePosition) {
        TaskGetCouponsList(page, true);
    }

    @Override
    public void Refresh(MaterialListView recyclerView) {
        TaskGetCouponsList(1, false);
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName()); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
        MobclickAgent.onResume(this);          //统计时长
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName()); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息
        MobclickAgent.onPause(this);
    }
}
